<title>「一本通 4.5 例 1」树的统计</title><link href="http://hzoi.com/tomorrow.css" rel="stylesheet"><div class="ui existing segment"><pre><code><span class="pl-cp">#include</span> <span class="pl-cpf">&lt;algorithm&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span> <span class="pl-cpf">&lt;cmath&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span> <span class="pl-cpf">&lt;cstdio&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span> <span class="pl-cpf">&lt;cstdlib&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span> <span class="pl-cpf">&lt;cstring&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span> <span class="pl-cpf">&lt;iostream&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#define MAXN 30005</span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-n">std</span><span class="pl-p">;</span>
<span class="pl-kt">void</span> <span class="pl-nf">getint</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-o">&amp;</span><span class="pl-n">num</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-kt">char</span> <span class="pl-n">ch</span><span class="pl-p">;</span>
    <span class="pl-kt">bool</span> <span class="pl-n">neg</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">isdigit</span><span class="pl-p">(</span><span class="pl-n">ch</span> <span class="pl-o">=</span> <span class="pl-n">getchar</span><span class="pl-p">()))</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">ch</span> <span class="pl-o">==</span> <span class="pl-sc">&#39;-&#39;</span><span class="pl-p">)</span> <span class="pl-n">neg</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-n">num</span> <span class="pl-o">=</span> <span class="pl-n">ch</span> <span class="pl-o">-</span> <span class="pl-sc">&#39;0&#39;</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">isdigit</span><span class="pl-p">(</span><span class="pl-n">ch</span> <span class="pl-o">=</span> <span class="pl-n">getchar</span><span class="pl-p">()))</span> <span class="pl-n">num</span> <span class="pl-o">=</span> <span class="pl-n">num</span> <span class="pl-o">*</span> <span class="pl-mi">10</span> <span class="pl-o">+</span> <span class="pl-n">ch</span> <span class="pl-o">-</span> <span class="pl-sc">&#39;0&#39;</span><span class="pl-p">;</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">neg</span><span class="pl-p">)</span> <span class="pl-n">num</span> <span class="pl-o">=</span> <span class="pl-o">-</span><span class="pl-n">num</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">q</span><span class="pl-p">,</span> <span class="pl-n">tope</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-kt">char</span> <span class="pl-n">opt</span><span class="pl-p">[</span><span class="pl-mi">15</span><span class="pl-p">];</span>
<span class="pl-kt">int</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">];</span>
<span class="pl-kt">int</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">dfstime</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">,</span> <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">],</span> <span class="pl-n">nw</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">];</span>
<span class="pl-kt">int</span> <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">MAXN</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">2</span><span class="pl-p">],</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">MAXN</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">2</span><span class="pl-p">];</span>
<span class="pl-cp">#define lc (u &lt;&lt; 1)</span>
<span class="pl-cp">#define rc (u &lt;&lt; 1 | 1)</span>
<span class="pl-kt">void</span> <span class="pl-nf">update</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">lc</span><span class="pl-p">]</span> <span class="pl-o">+</span> <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">rc</span><span class="pl-p">],</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">max</span><span class="pl-p">(</span><span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">lc</span><span class="pl-p">],</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">rc</span><span class="pl-p">]);</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">build</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">==</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">nw</span><span class="pl-p">[</span><span class="pl-n">l</span><span class="pl-p">];</span>
        <span class="pl-k">return</span><span class="pl-p">;</span>
    <span class="pl-p">}</span>
    <span class="pl-kt">int</span> <span class="pl-n">mid</span> <span class="pl-o">=</span> <span class="pl-n">l</span> <span class="pl-o">+</span> <span class="pl-n">r</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">);</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">mid</span> <span class="pl-o">&lt;</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span><span class="pl-p">);</span>
    <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">modify</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">pos</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">v</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-kt">int</span> <span class="pl-n">u</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">l</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span> <span class="pl-o">=</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-o">*</span><span class="pl-n">stk</span> <span class="pl-o">=</span> <span class="pl-k">new</span> <span class="pl-kt">int</span><span class="pl-p">[</span><span class="pl-mi">20</span><span class="pl-p">],</span> <span class="pl-n">tops</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-n">pos</span> <span class="pl-o">=</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">pos</span><span class="pl-p">];</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">&lt;</span> <span class="pl-n">r</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-n">stk</span><span class="pl-p">[</span><span class="pl-n">tops</span><span class="pl-o">++</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">u</span><span class="pl-p">;</span>
        <span class="pl-kt">int</span> <span class="pl-n">mid</span> <span class="pl-o">=</span> <span class="pl-n">l</span> <span class="pl-o">+</span> <span class="pl-n">r</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">pos</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span>
            <span class="pl-n">u</span> <span class="pl-o">=</span> <span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">r</span> <span class="pl-o">=</span> <span class="pl-n">mid</span><span class="pl-p">;</span>
        <span class="pl-k">else</span>
            <span class="pl-n">u</span> <span class="pl-o">=</span> <span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">l</span> <span class="pl-o">=</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-p">}</span>
    <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">v</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">tops</span><span class="pl-p">)</span> <span class="pl-n">update</span><span class="pl-p">(</span><span class="pl-n">stk</span><span class="pl-p">[</span><span class="pl-o">--</span><span class="pl-n">tops</span><span class="pl-p">]);</span>
    <span class="pl-k">delete</span><span class="pl-p">[]</span> <span class="pl-n">stk</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">query_sum</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">qr</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">==</span> <span class="pl-n">ql</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">r</span> <span class="pl-o">==</span> <span class="pl-n">qr</span><span class="pl-p">)</span> <span class="pl-k">return</span> <span class="pl-n">sum</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span>
    <span class="pl-kt">int</span> <span class="pl-n">mid</span> <span class="pl-o">=</span> <span class="pl-n">l</span> <span class="pl-o">+</span> <span class="pl-n">r</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">qr</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span>
        <span class="pl-k">return</span> <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">);</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">ql</span> <span class="pl-o">&gt;</span> <span class="pl-n">mid</span><span class="pl-p">)</span>
        <span class="pl-k">return</span> <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">);</span>
    <span class="pl-k">else</span>
        <span class="pl-k">return</span> <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">)</span> <span class="pl-o">+</span>
               <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">query_max</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">qr</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">l</span> <span class="pl-o">==</span> <span class="pl-n">ql</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">r</span> <span class="pl-o">==</span> <span class="pl-n">qr</span><span class="pl-p">)</span> <span class="pl-k">return</span> <span class="pl-n">mx</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span>
    <span class="pl-kt">int</span> <span class="pl-n">mid</span> <span class="pl-o">=</span> <span class="pl-n">l</span> <span class="pl-o">+</span> <span class="pl-n">r</span> <span class="pl-o">&gt;&gt;</span> <span class="pl-mi">1</span><span class="pl-p">;</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">qr</span> <span class="pl-o">&lt;=</span> <span class="pl-n">mid</span><span class="pl-p">)</span>
        <span class="pl-k">return</span> <span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">);</span>
    <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">ql</span> <span class="pl-o">&gt;</span> <span class="pl-n">mid</span><span class="pl-p">)</span>
        <span class="pl-k">return</span> <span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">);</span>
    <span class="pl-k">else</span>
        <span class="pl-k">return</span> <span class="pl-n">max</span><span class="pl-p">(</span><span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-n">lc</span><span class="pl-p">,</span> <span class="pl-n">l</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">,</span> <span class="pl-n">ql</span><span class="pl-p">,</span> <span class="pl-n">mid</span><span class="pl-p">),</span>
                   <span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-n">rc</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">r</span><span class="pl-p">,</span> <span class="pl-n">mid</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">qr</span><span class="pl-p">));</span>
<span class="pl-p">}</span>
<span class="pl-k">struct</span> <span class="pl-n">Edge</span> <span class="pl-p">{</span>
    <span class="pl-kt">int</span> <span class="pl-n">np</span><span class="pl-p">;</span>
    <span class="pl-n">Edge</span> <span class="pl-o">*</span><span class="pl-n">nxt</span><span class="pl-p">;</span>
<span class="pl-p">}</span> <span class="pl-n">E</span><span class="pl-p">[</span><span class="pl-n">MAXN</span> <span class="pl-o">&lt;&lt;</span> <span class="pl-mi">1</span><span class="pl-p">],</span> <span class="pl-o">*</span><span class="pl-n">V</span><span class="pl-p">[</span><span class="pl-n">MAXN</span><span class="pl-p">];</span>
<span class="pl-kt">void</span> <span class="pl-nf">addedge</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">v</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">E</span><span class="pl-p">[</span><span class="pl-o">++</span><span class="pl-n">tope</span><span class="pl-p">].</span><span class="pl-n">np</span> <span class="pl-o">=</span> <span class="pl-n">v</span><span class="pl-p">,</span> <span class="pl-n">E</span><span class="pl-p">[</span><span class="pl-n">tope</span><span class="pl-p">].</span><span class="pl-n">nxt</span> <span class="pl-o">=</span> <span class="pl-n">V</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">],</span> <span class="pl-n">V</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-o">&amp;</span><span class="pl-n">E</span><span class="pl-p">[</span><span class="pl-n">tope</span><span class="pl-p">];</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">dfs1</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">f</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">d</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">d</span><span class="pl-p">,</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">f</span><span class="pl-p">,</span> <span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-k">register</span> <span class="pl-n">Edge</span> <span class="pl-o">*</span><span class="pl-n">ne</span> <span class="pl-o">=</span> <span class="pl-n">V</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span> <span class="pl-n">ne</span><span class="pl-p">;</span> <span class="pl-n">ne</span> <span class="pl-o">=</span> <span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">nxt</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span> <span class="pl-o">==</span> <span class="pl-n">f</span><span class="pl-p">)</span> <span class="pl-k">continue</span><span class="pl-p">;</span>
        <span class="pl-n">dfs1</span><span class="pl-p">(</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">,</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">d</span> <span class="pl-o">+</span> <span class="pl-mi">1</span><span class="pl-p">);</span>
        <span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">+=</span> <span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">];</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">]</span> <span class="pl-o">&gt;</span> <span class="pl-n">sz</span><span class="pl-p">[</span><span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]])</span> <span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">;</span>
    <span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">dfs2</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">tp</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">tp</span><span class="pl-p">,</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-o">++</span><span class="pl-n">dfstime</span><span class="pl-p">,</span> <span class="pl-n">nw</span><span class="pl-p">[</span><span class="pl-n">dfstime</span><span class="pl-p">]</span> <span class="pl-o">=</span> <span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">])</span> <span class="pl-k">return</span><span class="pl-p">;</span>
    <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">],</span> <span class="pl-n">tp</span><span class="pl-p">);</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-k">register</span> <span class="pl-n">Edge</span> <span class="pl-o">*</span><span class="pl-n">ne</span> <span class="pl-o">=</span> <span class="pl-n">V</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">];</span> <span class="pl-n">ne</span><span class="pl-p">;</span> <span class="pl-n">ne</span> <span class="pl-o">=</span> <span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">nxt</span><span class="pl-p">)</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span> <span class="pl-o">!=</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">&amp;&amp;</span> <span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span> <span class="pl-o">!=</span> <span class="pl-n">hson</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">])</span> <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">,</span> <span class="pl-n">ne</span><span class="pl-o">-&gt;</span><span class="pl-n">np</span><span class="pl-p">);</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">path_sum</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">v</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-kt">int</span> <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">!=</span> <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">])</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]]</span> <span class="pl-o">&lt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">]])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">);</span>
        <span class="pl-n">res</span> <span class="pl-o">+=</span> <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]],</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]);</span>
        <span class="pl-n">u</span> <span class="pl-o">=</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]];</span>
    <span class="pl-p">}</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">&gt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">);</span>
    <span class="pl-n">res</span> <span class="pl-o">+=</span> <span class="pl-n">query_sum</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">],</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">]);</span>
    <span class="pl-k">return</span> <span class="pl-n">res</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">path_max</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-kt">int</span> <span class="pl-n">v</span><span class="pl-p">)</span> <span class="pl-p">{</span>
    <span class="pl-kt">int</span> <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-mh">0xc0c0c0c0</span><span class="pl-p">;</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">!=</span> <span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">])</span> <span class="pl-p">{</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]]</span> <span class="pl-o">&lt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">]])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">);</span>
        <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-n">max</span><span class="pl-p">(</span><span class="pl-n">res</span><span class="pl-p">,</span> <span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]],</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]));</span>
        <span class="pl-n">u</span> <span class="pl-o">=</span> <span class="pl-n">fa</span><span class="pl-p">[</span><span class="pl-n">top</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]];</span>
    <span class="pl-p">}</span>
    <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">]</span> <span class="pl-o">&gt;</span> <span class="pl-n">dep</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">])</span> <span class="pl-n">swap</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">);</span>
    <span class="pl-n">res</span> <span class="pl-o">=</span> <span class="pl-n">max</span><span class="pl-p">(</span><span class="pl-n">res</span><span class="pl-p">,</span> <span class="pl-n">query_max</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">,</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">u</span><span class="pl-p">],</span> <span class="pl-n">dfn</span><span class="pl-p">[</span><span class="pl-n">v</span><span class="pl-p">]));</span>
    <span class="pl-k">return</span> <span class="pl-n">res</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">main</span><span class="pl-p">()</span> <span class="pl-p">{</span>
    <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">n</span><span class="pl-p">);</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-k">register</span> <span class="pl-kt">int</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">&lt;</span> <span class="pl-n">n</span><span class="pl-p">;</span> <span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">;</span>
        <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">),</span> <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">v</span><span class="pl-p">);</span>
        <span class="pl-n">addedge</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">),</span> <span class="pl-n">addedge</span><span class="pl-p">(</span><span class="pl-n">v</span><span class="pl-p">,</span> <span class="pl-n">u</span><span class="pl-p">);</span>
    <span class="pl-p">}</span>
    <span class="pl-k">for</span> <span class="pl-p">(</span><span class="pl-k">register</span> <span class="pl-kt">int</span> <span class="pl-n">i</span> <span class="pl-o">=</span> <span class="pl-mi">1</span><span class="pl-p">;</span> <span class="pl-n">i</span> <span class="pl-o">&lt;=</span> <span class="pl-n">n</span><span class="pl-p">;</span> <span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span> <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">w</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">]);</span>
    <span class="pl-n">dfs1</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">0</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">),</span> <span class="pl-n">dfs2</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">);</span>
    <span class="pl-n">build</span><span class="pl-p">(</span><span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-mi">1</span><span class="pl-p">,</span> <span class="pl-n">n</span><span class="pl-p">),</span> <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">q</span><span class="pl-p">);</span>
    <span class="pl-k">while</span> <span class="pl-p">(</span><span class="pl-n">q</span><span class="pl-o">--</span><span class="pl-p">)</span> <span class="pl-p">{</span>
        <span class="pl-kt">int</span> <span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">;</span>
        <span class="pl-n">scanf</span><span class="pl-p">(</span><span class="pl-s">&quot;%s&quot;</span><span class="pl-p">,</span> <span class="pl-n">opt</span><span class="pl-p">),</span> <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">),</span> <span class="pl-n">getint</span><span class="pl-p">(</span><span class="pl-n">v</span><span class="pl-p">);</span>
        <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">opt</span><span class="pl-p">[</span><span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">==</span> <span class="pl-sc">&#39;M&#39;</span><span class="pl-p">)</span>
            <span class="pl-n">printf</span><span class="pl-p">(</span><span class="pl-s">&quot;%d</span><span class="pl-se">\n</span><span class="pl-s">&quot;</span><span class="pl-p">,</span> <span class="pl-n">path_max</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">));</span>
        <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">opt</span><span class="pl-p">[</span><span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">==</span> <span class="pl-sc">&#39;S&#39;</span><span class="pl-p">)</span>
            <span class="pl-n">printf</span><span class="pl-p">(</span><span class="pl-s">&quot;%d</span><span class="pl-se">\n</span><span class="pl-s">&quot;</span><span class="pl-p">,</span> <span class="pl-n">path_sum</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">));</span>
        <span class="pl-k">else</span> <span class="pl-k">if</span> <span class="pl-p">(</span><span class="pl-n">opt</span><span class="pl-p">[</span><span class="pl-mi">1</span><span class="pl-p">]</span> <span class="pl-o">==</span> <span class="pl-sc">&#39;H&#39;</span><span class="pl-p">)</span>
            <span class="pl-n">modify</span><span class="pl-p">(</span><span class="pl-n">u</span><span class="pl-p">,</span> <span class="pl-n">v</span><span class="pl-p">);</span>
    <span class="pl-p">}</span>
    <span class="pl-k">return</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
</cpde></pre></div>